package com.easypan.service.impl;

import com.easypan.component.RedisComponent;
import com.easypan.entity.config.AppConfig;
import com.easypan.entity.constants.Constants;
import com.easypan.entity.dto.SessionWebUserDto;
import com.easypan.entity.dto.SysSettingsDto;
import com.easypan.entity.dto.UserSpaceDto;
import com.easypan.entity.enums.UserStatusEnum;
import com.easypan.entity.po.FileInfo;
import com.easypan.entity.po.UserInfo;
import com.easypan.entity.query.FileInfoQuery;
import com.easypan.entity.query.UserInfoQuery;
import com.easypan.entity.vo.PaginationResultVO;
import com.easypan.exception.BusinessException;
import com.easypan.mappers.FileInfoMapper;
import com.easypan.mappers.UserInfoMapper;
import com.easypan.service.EmailCodeService;
import com.easypan.service.FileInfoService;
import com.easypan.service.UserInfoService;
import com.easypan.utils.StringTools;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.ibatis.reflection.ArrayUtil;
import org.apache.tomcat.jni.User;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper<UserInfo,UserInfoQuery> userInfoMapper;

    @Resource
    private EmailCodeService emailCodeService;


    @Resource
    private RedisComponent redisComponent;

    @Resource
    private AppConfig appConfig;

    @Resource
    private FileInfoMapper<FileInfo, FileInfoQuery> fileInfoMapper;


    @Resource
    private FileInfoService fileInfoService;



    @Override
    public List<UserInfo> findListByParam(UserInfoQuery param) {
        return null;
    }

    @Override
    public Integer findCountByParam(UserInfoQuery param) {
        return null;
    }

    @Override
    public PaginationResultVO<UserInfo> findListByPage(UserInfoQuery param) {
        return null;
    }

    @Override
    public Integer add(UserInfo bean) {
        return null;
    }

    @Override
    public Integer addBatch(List<UserInfo> listBean) {
        return null;
    }

    @Override
    public Integer addOrUpdateBatch(List<UserInfo> listBean) {
        return null;
    }

    @Override
    public Integer updateByParam(UserInfo bean, UserInfoQuery param) {
        return null;
    }

    @Override
    public Integer deleteByParam(UserInfoQuery param) {
        return null;
    }

    @Override
    public UserInfo getUserInfoByUserId(String userId) {
        UserInfo userInfo=this.userInfoMapper.selectByUserId(userId);
        return userInfo;
    }

    @Override
    public Integer updateUserInfoByUserId(UserInfo bean, String userId) {
        return null;
    }

    @Override
    public Integer deleteUserInfoByUserId(String userId) {
        return null;
    }

    @Override
    public UserInfo getUserInfoByEmail(String email) {
        return null;
    }

    @Override
    public Integer updateUserInfoByEmail(UserInfo bean, String email) {
        return null;
    }

    @Override
    public Integer deleteUserInfoByEmail(String email) {
        return null;
    }

    @Override
    public UserInfo getUserInfoByQqOpenId(String qqOpenId) {
        return null;
    }

    @Override
    public Integer updateUserInfoByQqOpenId(UserInfo bean, String qqOpenId) {
        return null;
    }

    @Override
    public Integer deleteUserInfoByQqOpenId(String qqOpenId) {
        return null;
    }

    @Override
    public UserInfo getUserInfoByNickname(String nickname) {
        return null;
    }

    @Override
    public Integer updateUserInfoByNickname(UserInfo bean, String nickname) {
        return null;
    }

    @Override
    public Integer deleteUserInfoByNickname(String nickname) {
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void  register(String email,String nickName,String password,String emailCode){

        UserInfo userInfo =this.userInfoMapper.selectByEmail(email);
        if(null!=userInfo){
            throw new BusinessException("邮箱账号已经存在");
        }

        UserInfo nickNameUser =this.userInfoMapper.selectByNickName(nickName);
        if(null!=nickNameUser){
            throw new BusinessException("昵称已经存在");
        }

//        校验邮箱验证码
        emailCodeService.checkCode(email,emailCode);
        String userId= StringTools.getRandomNumber(Constants.LENGTH_10);
        userInfo =new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setNickName(nickName);
        userInfo.setEmail(email);
        userInfo.setPassword(StringTools.encodeByMD5(password));
        userInfo.setJoinTime(new Date());
        userInfo.setStatus(UserStatusEnum.ENABLE.getStatus());
        userInfo.setUseSpace(0L);
        SysSettingsDto sysSettingsDto=redisComponent.getSysSettingsDto();
        userInfo.setTotalSpace((sysSettingsDto.getUserInitUseSpace()*Constants.MB));
        System.out.print(222232);
        System.out.print(email);
        System.out.print(nickName);
        System.out.print(emailCode);

        this.userInfoMapper.insert(userInfo);
    }
    @Override
    public SessionWebUserDto login(String email,String password){
        UserInfo userInfo =this.userInfoMapper.selectByEmail(email);
        if(null==userInfo||!userInfo.getPassword().equals(password)){
            throw new BusinessException("账号或密码错误");
        }
        if(UserStatusEnum.DISABLE.getStatus().equals(userInfo.getStatus())){
            throw new BusinessException("账号已禁用");
        }
        UserInfo updateInfo=new UserInfo();
        updateInfo.setLastLoginTime(new Date());
        this.userInfoMapper.updateByUserId(updateInfo,userInfo.getUserId());
        SessionWebUserDto sessionWebUserDto=new SessionWebUserDto();
        sessionWebUserDto.setNickName(userInfo.getNickName());
        sessionWebUserDto.setUserId(userInfo.getUserId());
        if(ArrayUtils.contains(appConfig.getAdminEmails().split(","),email)){
            sessionWebUserDto.setAdmin(true);
        }
        else{
            sessionWebUserDto.setAdmin(false);
        }
        UserSpaceDto userSpaceDto=new UserSpaceDto();
        Long useSpace=fileInfoMapper.selectUseSpace(userInfo.getUserId());
        userSpaceDto.setUseSpace(useSpace);
        userSpaceDto.setTotalSpace(userInfo.getTotalSpace());
        redisComponent.saveUserSpaceUse(userInfo.getUserId(),userSpaceDto);
        return sessionWebUserDto;
    }


    @Override
    public void resetPwd(String email, String password, String emailCode) {


        UserInfo userInfo =this.userInfoMapper.selectByEmail(email);

        if(null==userInfo){
            throw new BusinessException("此邮箱账号不存在哦");
        }

        // 校验邮箱验证码

        emailCodeService.checkCode(email,emailCode);

        UserInfo updateInfo=new UserInfo();
        updateInfo.setPassword(StringTools.encodeByMD5(password));
        this.userInfoMapper.updateByEmail(updateInfo, email);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserStatus(String userId, Integer status) {
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(status);
        if (UserStatusEnum.DISABLE.getStatus().equals(status)) {
            userInfo.setUseSpace(0L);
            fileInfoService.deleteFileByUserId(userId);
        }
        userInfoMapper.updateByUserId(userInfo, userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeUserSpace(String userId, Integer changeSpace) {
        Long space = changeSpace * Constants.MB;
        this.userInfoMapper.updateUserSpace(userId, null, space);
        redisComponent.resetUserSpaceUse(userId);
    }

}


























